﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KYJDAL
{
    public class ActualDataService
    {
        /// <summary>
        /// 实时数据存储
        /// </summary>
        /// <param name="varNameList"></param>
        /// <param name="varValueList"></param>
        /// <returns></returns>
        public int AddActualData(List<string> varNameList, List<string> varValueList)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("Insert into ActualData(");
            stringBuilder.Append(string.Join(",", varNameList));
            stringBuilder.Append(") Values(");
            stringBuilder.Append(string.Join(",", varValueList.Select(c => c = "'" + c + "'").ToList()));
            stringBuilder.Append(")");
            string str = stringBuilder.ToString();
            return SQLiteHelper.ExecuteNonQuery(stringBuilder.ToString());
        }

        /// <summary>
        /// 根据时间间隔和参数名称查询数据
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="columus"></param>
        /// <returns></returns>
        public DataTable QueryActualDataByTime(string start, string end, List<string> columus)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("Select InsertTime,");
            stringBuilder.Append(string.Join(",", columus));
            stringBuilder.Append(" from ActualData where InsertTime between @Start and @End");
            SQLiteParameter[] param = new SQLiteParameter[]
            {
                new SQLiteParameter("@Start",start),
                new SQLiteParameter("@End",end),
            };
            try
            {
                DataSet dataSet = SQLiteHelper.GetDataSet(stringBuilder.ToString(), param);
                if (dataSet.Tables.Count > 0)
                {
                    return dataSet.Tables[0];
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 查询报表数据，根据时间间隔和条件
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="condition"></param>
        /// <param name="dataTableName"></param>
        /// <returns></returns>
        public DataTable QueryRePortDataByCondition(string start, string end, List<string> condition, string dataTableName)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("select ");
            stringBuilder.Append(string.Join(",", condition));
            stringBuilder.Append(" from ActualData where InsertTime between @Start and @End");
            SQLiteParameter[] param = new SQLiteParameter[]
            {
                 new SQLiteParameter("@Start",start),
                 new SQLiteParameter("@End",end)
            };

            try
            {
                DataSet dataSet = SQLiteHelper.GetDataSet(stringBuilder.ToString(), param, dataTableName);
                if (dataSet.Tables.Count > 0)
                {
                    return dataSet.Tables[0];
                }
                else
                {
                    return null;
                }
                }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
